package com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.activity;

import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Toast;

import com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.R;
import com.app.adnroid.sichuanxiaoke.cn.xiaokebusiness.Utils.LogUtil;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.ui.RecognizerDialog;
import com.iflytek.cloud.ui.RecognizerDialogListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;

public class XunFeiVoiceActivity extends AppCompatActivity {

    @BindView(R.id.tb)
    Toolbar tb;

    private RecognizerDialog recognizerDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xun_fei_voice);
        ButterKnife.bind(this);
        // 初始化识别对象
        SpeechUtility.createUtility(this, SpeechConstant.APPID + "=59bf3b95");
        initUI();
    }

    private void initUI() {
        setSupportActionBar(tb);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        tb.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                finish();
            }
        });
    }

    public void XunfeiVoice(View view) {
        initVoice();
    }

    private void initVoice(){
        //1.创建RecognizerDialog对象
        recognizerDialog = new RecognizerDialog(this, null);
        //2.设置accent、language等参数
        recognizerDialog.setParameter(SpeechConstant.LANGUAGE, "zh_cn");//语种，这里可以有zh_cn和en_us
        recognizerDialog.setParameter(SpeechConstant.ACCENT, "mandarin");//设置口音，这里设置的是汉语普通话 具体支持口音请查看讯飞文档，
        recognizerDialog.setParameter(SpeechConstant.TEXT_ENCODING, "utf-8");//设置编码类型


        recognizerDialog.setListener(new RecognizerDialogListener() {
            @Override
            public void onResult(RecognizerResult recognizerResult, boolean b) {//返回结果
                if (!b) {
                    String result= recognizerResult.getResultString();
                    LogUtil.i("xunfeiResult", result);
                    JSONObject jsonObject=null;
                    StringBuilder builder=new StringBuilder();
                    try {
                        jsonObject = new JSONObject(result);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    JSONArray ws = jsonObject.optJSONArray("ws");
                    for (int i = 0; i < ws.length(); i++) {
                        JSONArray cw = ws.optJSONObject(i).optJSONArray("cw");
                        for (int j = 0; j < cw.length(); j++) {
                            String w = cw.optJSONObject(j).optString("w");
                            builder.append(w);
                        }

                    }

                    LogUtil.i("xunfeiResult", "builder == "+builder.toString());
                    VoiceAction(builder.toString());
                }
            }

            @Override
            public void onError(SpeechError speechError) {//返回错误
                LogUtil.e("xunfeiResult", speechError.getErrorCode() + "");
            }

        });
        if(recognizerDialog == null){
            LogUtil.e("xunfeiResult",  "recognizerDialog == null");
        }
        //显示讯飞语音识别视图
        recognizerDialog.show();
    }

    private void VoiceAction(String order){

        int music = order.indexOf("音乐");
        int call = order.indexOf("打电话");
        int goInternet = order.indexOf("上网");
        LogUtil.i("xunfeiResult"," music="+music+"  call="+call+" goInternet="+goInternet);
        if(music>=0){
            Intent intent = new Intent("android.intent.action.MUSIC_PLAYER");
            try{
                startActivity(intent);
            }catch (Exception e){
                Toast.makeText(XunFeiVoiceActivity.this,"没有找到系统播放器",Toast.LENGTH_SHORT).show();
            }

            return;

        }
        if(call>=0){
            Intent intent =  new Intent(Intent.ACTION_CALL_BUTTON);//跳转到拨号界面
            try{
                startActivity(intent);
            }catch (Exception e){
                Toast.makeText(XunFeiVoiceActivity.this,"跳转失败",Toast.LENGTH_SHORT).show();
            }
            return;
        }
        if(goInternet>=0){
            Uri uri = Uri.parse("https://www.baidu.com");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            try{
                startActivity(intent);
            }catch (Exception e){
                Toast.makeText(XunFeiVoiceActivity.this,"没有找到系统浏览器",Toast.LENGTH_SHORT).show();
            }
            return;
        }
    }
}
